package com.xh.fma.commodity.service.impl;

import com.xh.fma.commodity.dao.CategoryBrandRelationDao;
import com.xh.fma.commodity.entity.CategoryBrandRelationEntity;
import com.xh.fma.commodity.service.CategoryBrandRelationService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xh.common.utils.PageUtils;
import com.xh.common.utils.Query;

import com.xh.fma.commodity.dao.BrandDao;
import com.xh.fma.commodity.entity.BrandEntity;
import com.xh.fma.commodity.service.BrandService;

import javax.annotation.Resource;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Resource
    private CategoryBrandRelationDao categoryBrandRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                new QueryWrapper<BrandEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<BrandEntity> listBrands(Long catId) {
        List<CategoryBrandRelationEntity> categoryBrandRelationEntities =
                categoryBrandRelationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("category_id", catId));
        List<Long> collect =
                categoryBrandRelationEntities.stream().map(CategoryBrandRelationEntity::getBrandId).collect(Collectors.toList());
        List<BrandEntity> brandEntities =
                this.baseMapper.selectBatchIds(collect);
        return brandEntities;
    }

}